﻿using Avalon.Core.Data.Templates;
using System;
using System.Collections.Generic;
using System.Data.Entity;

namespace Avalon.Core.Data {

    public class AvalonDbInitializer<T> : DropCreateDatabaseIfModelChanges<T> where T : BaseDbContext {

        public event EventHandler OnDatabaseCreated;

        protected override void Seed(T context) {
            AvalonViewRendererGroup group = new AvalonViewRendererGroup() {
                ControllerName = "null",
                DefaultTemplate = "VM{0}",
                Name = "Default",
                IsDefault = true,
                Description = "The default view renderer group for the Avalon framework. This record is automatically generated when recreating the database.",
                AvalonViewRenderers = new List<AvalonViewRenderer>() {
                    new AvalonViewRenderer() { Action = "Index", Name = "Index", Renderer = "Avalon.Core.MVC.Helpers.TableHelper", Template = "VM{0}_IndexItem", GetPipeline = "GetAll", PostPipeline = "null" },
                    new AvalonViewRenderer() { Action = "Detail", Name = "Detail", Renderer = "Avalon.Core.MVC.Helpers.DetailFormHelper", Template = "VM{0}_DetailItem", GetPipeline = "Get", PostPipeline = "null" },
                    new AvalonViewRenderer() { Action = "Edit", Name = "Edit", Renderer = "Avalon.Core.MVC.Helpers.EditFormHelper", Template = "VM{0}_EditItem", GetPipeline = "Get", PostPipeline = "SaveExisting" },
                    new AvalonViewRenderer() { Action = "Create", Name = "Create", Renderer = "Avalon.Core.MVC.Helpers.CreateFormHelper", Template = "VM{0}_CreateItem", GetPipeline = "GetNew", PostPipeline = "SaveNew" },
                    new AvalonViewRenderer() { Action = "CreateReferencedEntity", Name = "CreateReferencedEntity", Renderer = "Avalon.Core.MVC.Helpers.CreateFormHelper", Template = "VM{0}_CreateItem", GetPipeline = "GetNew", PostPipeline = "SaveNew" }
                }
            };

            context.ViewRendererGroups.Add(group);
            context.SaveChanges();

            if (OnDatabaseCreated != null) {
                OnDatabaseCreated(this, null);
            }
        }
    }
}
